package ihm;

import java.util.Scanner;

/**
 * The parser has a set of known command words. It checks user input against the
 * known commands, and if the input is not one of the known commands, it returns
 * a command object that is marked as an unknown command.
 * 
 * @author Michael Kolling, David J. Barnes and Cyril GORRIERI
 * @version 2012.01.24
 */
public class Parser {
    private Scanner reader; // source of command input

    /**
     * Create a parser to read from the terminal window.
     */
    public Parser() {
        reader = new Scanner(System.in);
    }

    /**
     * @return The next command from the user.
     */
    public Command getCommand() {
        String inputLine; // will hold the full input line
        String word1 = null;
        String word2 = null;
        String word3 = null;

        System.out.print("$> "); // print prompt

        inputLine = reader.nextLine();

        // Find up to two words on the line.
        Scanner tokenizer = new Scanner(inputLine);
        if (tokenizer.hasNext()) {
            word1 = tokenizer.next(); // get first word
            if (tokenizer.hasNext()) {
                word2 = tokenizer.next(); // get second word
                if (tokenizer.hasNext()) {
                    word3 = tokenizer.next(); // get second word
                    // note: we just ignore the rest of the input line.
                }
            }
        }

        return new Command(CommandWordHelper.getCommandWord(word1), word2, word3);
    }

    /**
     * Print out a list of valid command words.
     */
    public void showCommands() {
    	CommandWordHelper.showAll();
    }
}
